<?php

namespace app\index\controller;

use think\Controller;
use think\Db;

class Plan extends Controller
{
    /**
     * 每天0点初始化商品库存
     * */
    public function initGoodsStock()
    {
        $list = Db::name('goods')->field('id,stock_count,copy_stock_count')->select();
        if ($list) {
            $time = time();
            foreach ($list as &$v) {
                Db::name('goods')->where(['id' => $v['id']])->update(['stock_count' => $v['copy_stock_count'], 'update_time' => $time]);
            }
        }
    }

    /**
     * 每天0点初始化实物券抽奖数量
     * */
    public function initLotteryCouponCount()
    {
        $list = Db::name('lottery_coupon')->field('id,count,copy_count')->select();
        if ($list) {
            $time = time();
            foreach ($list as &$v) {
                Db::name('lottery_coupon')->where(['id' => $v['id'], 'is_del' => 0])->update(['count' => $v['copy_count'], 'update_time' => $time]);
            }
        }
    }

    /**
     * 自动配送【非预订单支付成功超过3分钟】
     * */
    public function orderAutoSend()
    {
        $time = time() - 180;

        $list = Db::name('order')->field('id,user_id,shop_id')->where(['is_pay' => 1, 'status' => 2, 'is_business' => 0, 'pay_time' => ['elt', $time]])->select();
        $business_list = Db::name('order')->field('id,user_id,shop_id,arrive_time')->where(['is_pay' => 1, 'status' => 2, 'is_business' => 1])->select();

        $ids = '';
        $data_msg = [];
        $time = time();

        Db::startTrans();
        try {
            if ($list) {
                foreach ($list as &$v) {
                    if ($ids == '') {
                        $ids = $v['id'];
                    } else {
                        $ids .= ',' . $v['id'];
                    }

                    $data_msg[] = [
                        "user_id" => $v["user_id"],
                        "shop_id" => $v['shop_id'],
                        "order_id" => $v['id'],
                        "title" => "您的订单已经开始配送",
                        "content" => "您的订单已经开始配送",
                        "create_time" => $time,
                    ];
                }
            }

            if ($business_list) {
                foreach ($business_list as &$v) {
                    if (isTimeInRange(date('Y-m-d H:i'), $v['arrive_time'])) {
                        if ($ids == '') {
                            $ids = $v['id'];
                        } else {
                            $ids .= ',' . $v['id'];
                        }

                        $data_msg[] = [
                            "user_id" => $v["user_id"],
                            "shop_id" => $v['shop_id'],
                            "order_id" => $v['id'],
                            "title" => "您的订单已经开始配送",
                            "content" => "您的订单已经开始配送",
                            "create_time" => $time,
                        ];
                    }
                }
            }

            if ($ids) {
                Db::name('order')->where(['id' => ['IN', $ids]])->update(['status' => 3, 'update_time' => $time]);

                Db::name("msg")->insertAll($data_msg);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }
}
